package com.koron.standard.deviceClasssify.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koron.standard.deviceClasssify.bean.po.TypeParams;
import com.koron.standard.deviceClasssify.mapper.TypeParamsMapper;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 设备分类标准参数从表(TypeParams)表服务实现类
 *
 * @author jzp
 * @since 2021-07-13 17:13:26
 */
@Service
public class TypeParamsService extends ServiceImpl<TypeParamsMapper, TypeParams> {

    /**
     * 查询出该类型所有的参数
     */
    public List<TypeParams> listByTypeId(String typeId) {
        LambdaQueryWrapper<TypeParams> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TypeParams::getTypeId, typeId);
        wrapper.orderByAsc(TypeParams::getSort);
        return list(wrapper);
    }

    public List<TypeParams> listByTypeIds(List<String> typeIds) {
        LambdaQueryWrapper<TypeParams> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(TypeParams::getTypeId, typeIds);
        wrapper.orderByAsc(TypeParams::getSort);
        return list(wrapper);
    }

    /**
     * 保存技术参数
     */
    public void saveParams(Collection<TypeParams> entityList, String typeId) {
        // 参数名称唯一
        long count = entityList.stream().map(TypeParams::getParamName).distinct().count();
        if (entityList.size() > count) {
            throw new IllegalArgumentException("参数名重复");
        }

        // 1. 删除该类型的所有技术参数
        removeByTypeId(typeId);

        // 2.
        AtomicInteger sortNo = new AtomicInteger(0);
        for (TypeParams typeParams : entityList) {
            if (typeParams.getTypeId() == null) {
                typeParams.setTypeId(typeId);
            }
            if (typeParams.getGroup() == null) {
                typeParams.setGroup("2");
            }
            typeParams.setSort(sortNo.getAndIncrement());
        }

        // 3. 插入
        saveBatch(entityList);
    }

    /**
     * 删除该类型的所有技术参数
     */
    public void removeByTypeId(String typeId) {
        if (StrUtil.isEmpty(typeId)) {
            return;
        }
        LambdaQueryWrapper<TypeParams> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TypeParams::getTypeId, typeId);
        List<TypeParams> list = list(wrapper);
        removeByIds(list.stream().map(TypeParams::getId).collect(Collectors.toList()));
    }

    public void fixParams() {
        TypeParamsMapper baseMapper = getBaseMapper();
        List<TypeParams> typeParams = baseMapper.selectListDataTextNoJson();
        for (TypeParams typeParam : typeParams) {
            String[] split = typeParam.getDataText().split(",");
            JSONArray jsonArray = new JSONArray();
            for (int i = 0; i < split.length; i++) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.set("value", i + "");
                jsonObject.set("label", split[i]);
                jsonArray.put(jsonObject);
            }
            typeParam.setDataText(JSONUtil.toJsonStr(jsonArray));
        }
        updateBatchById(typeParams);
    }

}